//
// Description: 121. 买卖股票的最佳时机
// Created by Loading on 2021/2/10.
//

#include <bits/stdc++.h>

using namespace std;

int maxProfit(vector<int> &prices) {
    if (prices.size() < 2) {
        return 0;
    }
    int maxprofit = 0;
    //用 i 记录最低价格的下标
    for (int i = 0, j = 1; j < prices.size(); j++) {
        if (prices[j] < prices[i]) {//找到更低的价格
            i = j;
        }else {//判断是否比之前计算的利润大，如果利润更大则更新
            maxprofit = max(prices[j] - prices[i], maxprofit);
        }
    }
    return maxprofit;
}

int main() {
    vector<int> v = {7, 1, 5, 3, 6, 4};
    cout << maxProfit(v) << endl;
}